package com.cxf.cxfuserdemo.controller;

import com.cxf.cxfuserdemo.feign.OrderFeign;
import com.cxf.cxfuserdemo.feign.StoreFeignClient;
import com.cxf.cxfuserdemo.repository.Order;
import com.google.common.collect.Maps;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.utils.FallbackMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.Map;

@RequestMapping
@RestController
public class UserController {


    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private StoreFeignClient storeFeignClient;

    @GetMapping("/user/{id}")
   @HystrixCommand(fallbackMethod="getOrderInfoBack",ignoreExceptions = {IOException.class,ArithmeticException.class})
    public Order getOrderInfo(@PathVariable("id") String id){
        int i = 1/0;  // ArithmeticException.class 异常不进入fallback
        Order forObject = restTemplate.getForObject("http://cxf-order-demo/order/"+id, Order.class);
        System.out.println("收到响应信息：...."+forObject);
        return forObject;
    }

    public Order getOrderInfoBack(String id,Throwable throwable){
        logger.error("i am wrong",throwable);
        Order order = new Order();
        order.setOrderId("1234");
        order.setProductId("4321");
        order.setProductPrice("12.33");
        order.setProductName("美汁源");
        return order;
    }


    @GetMapping("/userFeign/{id}")
    public Order getOrderInfoFeign(@PathVariable("id") String id){
        Order forObject = orderFeign.queryOrder(id);
        System.out.println("feign 收到响应信息：...."+forObject);
        return forObject;
    }

    @GetMapping("/userFeignDiff/{id}")
    public Order getOrderInfoFeignDiffName(@PathVariable("id") String id){
        Order forObject = orderFeign.queryOrderFeign(id);
        System.out.println("FeignDiffName 收到响应信息：...."+forObject);
        return forObject;
    }

  //  @HystrixCommand(fallbackMethod = "getEchoInfoBack",ignoreExceptions = {IOException.class,ArithmeticException.class})
    @GetMapping("/echoMes/{msg}")
    public String getEchoInfo(@PathVariable("msg") String msg){
        String msgInfo = storeFeignClient.echoYourMessageFromUserDemo(msg);
        return msgInfo;
    }

    public String getEchoInfoBack(String msg,Throwable throwable){
        logger.error("i am wrong",throwable);

        return "getEchoInfoBack";
    }



   // GET  http://169.254.249.67:9933/login?user=cxf&pwd=123456
    @GetMapping("/login")
    public String loginSome(@RequestParam("user") String user, @RequestParam("pwd") String pwd){
        String msg = storeFeignClient.echoYourMessage(user,pwd);
        return msg+"----"+user+"--"+pwd;
    }

    @GetMapping("/loginByMap")
    public String loginByMap(@RequestParam("user") String user, @RequestParam("pwd") String pwd){
        Map<String,Object> map = Maps.newHashMap();
        map.put("user",user);
        map.put("pwd",pwd);
        String msg = storeFeignClient.loginByMap(map);

        return "loginByMap : "+msg+"----"+user+"--"+pwd;
    }

    @GetMapping("/balance")
    public String getClientName(){
        ServiceInstance serviceInstance = loadBalancerClient.choose("cxf-store-demo");
        String host = serviceInstance.getHost();
        String uri = serviceInstance.getUri().toString();
        int port = serviceInstance.getPort();
        String urlStr = "收到：...host:"+host+"--Uri:"+uri+"--Port:"+port;
        System.out.println(urlStr);
        return urlStr;
    }

    // 未注册到服务器的服务
    @GetMapping("/echo/{id}")
    public String getStoreInfo(@PathVariable("id") String id){
        String forObject = restTemplate.getForObject("http://cxf-store-demo/echo/"+id, String.class);
        System.out.println("收到响应信息：...."+forObject);
        return forObject;
    }

}
